import React, { memo, useEffect, useState } from "react";
import HeaderRightWarrper from "./style";

import IconAvatar from "@/assets/svg/icon_avatar";
import IconGlobal from "@/assets/svg/icon_global";
import IconMenu from "@/assets/svg/icon_menu";

const headerRight = memo(() => {
  const [showPanel, setShowPanel] = useState(false);

  const handlerShow = () => {
    setShowPanel(false);
  };

  useEffect(() => {
    //设置为true为捕获事件
    window.addEventListener("click", handlerShow, true);

    return () => {
      window.removeEventListener("click", handlerShow, true);
    };
  }, []);

  const profileClickHandle = () => {
    setShowPanel(true);
  };

  return (
    <HeaderRightWarrper>
      <div className="btns">
        <span className="btn">登录</span>
        <span className="btn">注册</span>
        <span className="btn">
          <IconGlobal />
        </span>
      </div>

      <div className="profile" onClick={profileClickHandle}>
        <IconMenu />
        <IconAvatar />

        {showPanel && (
          <div className="panel">
            <div className="top">
              <div className="item register">注册</div>
              <div className="item login">登录</div>
            </div>
            <div className="bottom">
              <div className="item">出租房源</div>
              <div className="item">开展体验</div>
              <div className="item">帮助</div>
            </div>
          </div>
        )}
      </div>
    </HeaderRightWarrper>
  );
});

export default headerRight;
